<?php defined('SYSPATH') or die('No direct script access.');

/**
 * 基础控制器
 * 
 * @author 陈蕾
 */
class Controller_Base extends Controller
{
	/**
	 * 调试开关
	 */
	const DEBUG = FALSE;
	
	/**
	 * 输出内容的基本结构
	 * 
	 * @var array
	 */
	private $_out_base = array(
		'has_error' => 0,
		'error'     => NULL,
		'data'      => NULL
	);
	
	/**
	 * 用户ID
	 */
	protected $_id;
	
	/**
	 * 经过解析后的外部数据
	 * 
	 * @var array
	 */
	protected $_params;
	
	//==========================================================================
	// Hooks
	//==========================================================================
	
	public function before()
	{
		// 解析客户端传入的数据
		$this->_params = json_decode(Arr::get($_POST, 'json'), TRUE);
		if (is_null($this->_params)) {
			$this->_params = array();
		}
		
		// 调试数据
		if (self::DEBUG) {
			$this->id = 1;
		} else {
			$this->id = $this->get('id');
		}
	}
	
	//==========================================================================
	// Actions
	//==========================================================================
	
	/**
	 * 缺省
	 */
	public function action_index()
	{
		$this->request->response = '《智能导学》后端程序。';
	}
	
	//==========================================================================
	// Utils
	//==========================================================================
	
	/**
	 * 获取参数的某个字段名
	 * 
	 * @param string $name 字段名
	 */
	protected function get($name)
	{
		return Arr::get($this->_params, $name);
	}
	
	/**
	 * 设置测试数据
	 * 
	 * @param array $data 测试数据，值会被追加到$this->_params里
	 */
	protected function test(Array $data)
	{
		if (self::DEBUG) {
			foreach ($data as $field => $value) {
				if (array_key_exists($field, $this->_params)) {
					throw new Exception("测试数据重复！字段名：{$field}");
				}
				$this->_params[$field] = $value;
			}
		}
	}
	
	/**
	 * 输出错误
	 * 
	 * @param string $text 错误信息
	 */
	protected function error($text)
	{
		$result = $this->_out_base;
		$result['has_error'] = 1;
		$result['error']     = $text;
		
		// 调试时使用结构化的输出
		if (self::DEBUG) {
			$this->debug($result);
			return;
		}
		$this->request->response = json_encode($result);
	}
	
	/**
	 * 输出结果
	 * 
	 * @param array $data 数据
	 */
	protected function output($data)
	{
		$result = $this->_out_base;
		$result['data'] = $data;
		
		if (self::DEBUG) {
			$this->debug($result);
			return;
		}
		
		$this->request->response = json_encode($result);
	}
	
	/**
	 * 调试输出
	 * 
	 * @param mixed $var 任意类型的变量
	 */
	protected function debug($var)
	{
		$dump = print_r($var, TRUE);
		$this->request->response = "<pre>{$dump}</pre>";
	}
	
	/**
	 * 精简结果
	 * 
	 * @param array $source 源结果
	 * @param array $fields 所需的字段集合
	 * @throws Exception
	 */
	protected function reduceResult($source, $fields)
	{
		$ret = array();
		foreach ($fields as $field) {
			if (!array_key_exists($field, $source)) {
				throw new Exception("数据键值 {$field} 不存在！");
			}
			$ret[$field] = $source[$field];
		}
		return $ret;
	}
}